package com.iterator.service.wechat.lc;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iterator.bean.web.pojo.tcy.CommentExample;
import com.iterator.bean.wechat.lc.WxCommentListBO;
import com.iterator.bean.wechat.lc.WxCommentListItem;
import com.iterator.bean.wechat.lc.WxCommentListItemVo;
import com.iterator.mapper.web.goodsManager.CommentMapper;
import com.iterator.mapper.wechat.lc.CutstomCommentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Service
public class WxCommentServiceImpl implements WxCommentService{
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    CutstomCommentMapper cutstomCommentMapper;
    @Override
    public Map<String, Object> count(Integer valueId, Integer type) {

        CommentExample commentExample = new CommentExample();
        //全部评论数目
        CommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andDeletedEqualTo(false)
                .andValueIdEqualTo(valueId)
                .andTypeEqualTo(Byte.valueOf(type.toString()));
        long allCount = commentMapper.countByExample(commentExample);
        //有图评论数目
        CommentExample.Criteria criteria1 = commentExample.createCriteria();
        criteria.andDeletedEqualTo(false)
                .andValueIdEqualTo(valueId)
                .andTypeEqualTo(Byte.valueOf(type.toString()))
                .andHasPictureEqualTo(true);
        long hasPicCount = commentMapper.countByExample(commentExample);
        Map<String, Object> map = new HashMap<>();
        map.put("allCount",allCount);
        map.put("hasPicCount",hasPicCount);
        return map;
    }

    @Override
    public Map<String, Object> list(WxCommentListBO commentListBO) {
        PageHelper.startPage(commentListBO.getPage(),commentListBO.getSize());
        List<WxCommentListItem> wxCommentListItems =
                cutstomCommentMapper.selectCommentList(commentListBO);
        LinkedList<WxCommentListItemVo> wxCommentListItemVos = new LinkedList<>();
        for (WxCommentListItem wxCommentListItem : wxCommentListItems) {
            wxCommentListItemVos.add(new WxCommentListItemVo(
                    wxCommentListItem.getUserInfo(),
                    wxCommentListItem.getAddTime(),
                    wxCommentListItem.getContent(),
                    wxCommentListItem.getPicList()
            ));
        }
        PageInfo<WxCommentListItem> pageInfo = new PageInfo<>(wxCommentListItems);
        long count = pageInfo.getTotal();
        int currentPage = pageInfo.getPageNum();
        HashMap<String, Object> map = new HashMap<>();
        map.put("count",count);
        map.put("currentPage",currentPage);
        map.put("data", wxCommentListItemVos);
        return map;

    }
}
